/*
 *   TQ2440 camera test program
 *
 *   preview : 320x240 overlay on 320x240 16bpp LCD
 *
 *   TFT LCD size : 320x240
 */
#include <sys/time.h>
#include <sys/types.h>
#include <asm/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/videodev2.h>
#include <stdio.h>
#include <sys/time.h>
#define PIXFMT_NUM			5
#define INPUT_NUM 			5
#define CTRL_NUM 			100

#define V4L2_DEV_NODE			"/dev/ov7670"
#define BufSize (640 * 480 * 2)
#define GetFpsTimes 6

static void v4l2_show_on_fb(int fd) {
	int i = 0;
	float fps;
	int ret;
	char preview_buf[BufSize];
	char gchr = 10;
	struct timeval tv, tv2;
	gettimeofday(&tv, NULL);
    int j = GetFpsTimes;
	while(1) {
		if((ret = read(fd, &preview_buf, BufSize)) < 0) {
			printf("read error!");
			return;
		}
		// send_buffer(preview_buf, 640*480*2);
		// usleep(30*1000);
		i = i + 1;
		if(i == 60) {
			gettimeofday(&tv2, NULL);
			fps = i / (((tv2.tv_sec - tv.tv_sec) * 1000 + (tv2.tv_usec - tv.tv_usec) / 1000) / (float)1000);
			printf("fps: %f\n", fps);
			gettimeofday(&tv, NULL);
			i = 0;
            j++;
            if(j <= 0)
                break;
		}
	}
}

int main(int argc, char *argv[]) {
    int v4l2_fd = -1;
	int fb_fd = -1;
	char *fbmem = NULL;
	unsigned int fb_length = 0;

	int preview_frames = 180;
	int tmp;

	printf("ov7670 Camera Test Program, Start !\n");
	v4l2_fd = open(V4L2_DEV_NODE, O_RDWR);
	if(v4l2_fd < 0) {
		perror(V4L2_DEV_NODE);
		goto out;
	}
	printf("read begining\n");
	v4l2_show_on_fb(v4l2_fd);

	printf("\n");
 out:

	if(v4l2_fd > 0)
		close(v4l2_fd);

	return 0;
}
